<?php

namespace Home\Controller;

use Think\Controller;

//加载commons下的functions.php自定义函数库
load("@.functions");

class ApiController extends Controller {

    public function index() {
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');

        $this->display();
    }

    public function user_api() {
//        show_bug($this);
        $this->display();
    }

    /*
    public function queryErrorCode() {
        //echo create_uuid();
        $level = $_GET['level']; //_不填  public公共 | 服务级1  | 系统级2
        $controller_class = $_GET['controller_class'];
        $controller_class_method = $_GET['controller_class_method'];
        //$arg = $_GET['apiClassId'];
        $error_code = M();
        if ($level == "public") {
            //长度为3  公共错误码
            $sql = "select * from dy_error_code where code like '___' order by code";
            $list = $error_code->query($sql);
//            show_bug($list);
            echo json_encode($list, JSON_PRETTY_PRINT);
        } else {
            $sql = "select * from dy_error_code where code like '" . $level . $controller_class . $controller_class_method . "__' order by code";
            //echo $sql;
            $list = $error_code->query($sql);
            echo json_encode($list, JSON_PRETTY_PRINT);
        }
    }
*/
    /*
    public function addErrorCode() {
        $code = $_GET['code'];
        $description = $_GET['description'];

        $sql = "select * from dy_error_code where code like '" . $code . "__'";
        //echo $sql.'<br/>';
        $error_code1 = M("error_code");
        $result = $error_code1->order("code")->where("code like '" . $code . "__'");
        // $total = $result->count(); //查出当前类下的方法数
        $r = $result->select();
        $result = $error_code1->order("code")->where("code like '" . $code . "__'");
        $total = $result->count(); //总记录数
        $last_code_id = $r[$total - 1]['code_id'];
        //echo $result -> getLastSql();
//        echo $total;
//        die();
        //以下算法实现code_id连续递增
        $error_code_id = $total + 1;
        //echo $total;
        if ($last_code_id > $total) {
            //echo '中间可以插入';
            for ($i = 0; $i < $total; $i++) {
//                echo $r[$i]['code_id'] . '<br/>';
                if ($r[$i]['code_id'] > $i + 1) {
                    $error_code_id = $i + 1;
                    break;
                }
            }
        }
        if ($error_code_id < 10) {
            $code = $code . '0' . $error_code_id;
        } else {
            $code = $code . "" . $error_code_id;
        }
//        echo $code;
//        die();
        $error_code = D("error_code");
        $error_code->code = $code;
        $error_code->description = $description;
        $error_code->code_id = $error_code_id;
        $result = $error_code->add();
//        echo $error_code->getLastSql();
//        die();
        if ($result > 0) {
            echo '{"info":"1","code":"' . $code . '","description":"' . $description . '"}';
        } else {
            echo '{"info":"0"}';
        }
    }
*/
    /*
    public function deleteErrorCode() {
        $code = $_GET['code'];
        $error_code = M('error_code');
        $result = $error_code->where("code='" . $code . "'")->delete(); //与上面效果相同，也是删除id为2的数据
        echo $result;
    }
*/
    public function queryMethodName() {
        $controller_class = $_GET['controller_class'];
//        echo $controller_class;
        $controller_class_method = M('Controller_class_method');
        $result = $controller_class_method->where("class_id='" . $controller_class . "'")->select(); //与上面效果相同，也是删除id为2的数据
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    function addApiClass() {
        $ApiClass = $_GET['ApiClassInput'];
        $ApiClassNote = $_GET['ApiClassNoteInput'];
        $controller_class = M("controller_class");
        $r = $controller_class->order("class_id");
        $result = $r->select();
//        print_r($result[0]);
//        echo count($result);
        $class_id = count($result) + 1;
        for ($i = 0; $i < count($result); $i++) {
//            echo $result[$i]['class_id'];
            if ($result[$i]['class_id'] > $i + 1) {
                $class_id = $i + 1;
                break;
            }
        }

        $controller_class = D("controller_class");
        $controller_class->class_name = $ApiClass;
        $controller_class->note = $ApiClassNote;
        $controller_class->class_id = $class_id;
        $result = $controller_class->add();
        if ($result > 0) {
            echo '{"info":"1","class_name":"' . $ApiClass . '","note":"' . $ApiClassNote . '", "class_id":"' . $class_id . '"}';
        } else {
            echo '{"info":"0"}';
        }
    }

    function queryApiClass() {
        $controller_class = M("controller_class");
        $r = $controller_class->order("class_id");
        $result = $r->select();
        echo json_encode($result, JSON_PRETTY_PRINT);
    }

    /**
     * 这个方法还存在问题
     */
    
    function addMethodToApiClass() {
        $class_id = $_GET['class_id'];
        $method_name = $_GET['method_name'];
        $note = $_GET['note'];
        //要写算法确定method_id
        //要写算法确定method_id
        $controller_class_method = M("controller_class_method");
        $r = $controller_class_method->where("class_id = ".$class_id)->order("method_id");
        $result = $r->select();
        $method_id = count($result) + 1;
        for ($i = 0; $i < count($result); $i++) {
            if ($result[$i]['method_id'] > $i + 1) {
                $method_id = $i + 1;
                break;
            }
        }
        $m=M('controller_class_method');
        $m->class_id = $class_id;
        $m->note = $note;
        $m->method_name = $method_name;
        $m->method_id = $method_id;
        $result = $m->add();
        if ($result > 0) {
            echo '{"info":"1","method_name":"' . $method_name . '","method_id":"' . $method_id . '", "class_id":"' . $class_id . '", "note":"' . $note . '"}';
        } else {
            echo '{"info":"0"}';
        }
    }

    function deleteApiClass() {
        $type = $_GET['type'];
        if ($type == "method") {
            $method_id = $_GET['method_id'];
            $controller_class_method = M('controller_class_method');
            $result = $controller_class_method->where("method_id='" . $method_id . "'")->delete(); //与上面效果相同，也是删除id为2的数据
            echo '{"info":' . $result . '}';
        } else if ($type == "class") {
            $class_id = $_GET['class_id'];
            $controller_class = M('controller_class');
            $result = $controller_class->where("class_id='" . $class_id . "'")->delete(); //与上面效果相同，也是删除id为2的数据
            echo '{"info":' . $result . '}';
        }
    }

}
